package com.zichen.pr.framework.web.service;

import com.zichen.pr.common.core.domain.entity.SysUser;
import com.zichen.pr.system.service.SysMenuService;
import com.zichen.pr.system.service.SysRoleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: zichen
 * @description: 用户权限处理
 * @author: rzhang
 * @create: 2021-04-08 17:56
 */
@Component
public class SysPermissionService {

    @Resource(name = "sysMenuService")
    private SysMenuService sysMenuService;

    @Resource(name = "sysRoleService")
    private SysRoleService sysRoleService;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(sysRoleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            perms.addAll(sysMenuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }
}
